//
//  HHProvinceController.m
//  HH快医轮播
//
//  Created by 虎虎 on 16/5/23.
//  Copyright © 2016年 SKY. All rights reserved.
//

#import "HHProvinceController.h"
#import "HHProvince.h"
#import "HHCitiesTableViewController.h"
@interface HHProvinceController ()<UITableViewDataSource,UITableViewDelegate>

@end

static NSString *ID = @"province";

@implementation HHProvinceController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.provinceArr.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    //设置cell的数据
    HHProvince *pro = self.provinceArr[indexPath.row];
    cell.textLabel.text = pro.name;
    return  cell;

}

#pragma mark - tableview代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HHCitiesTableViewController *citiesControl = [[HHCitiesTableViewController alloc] init];
    HHProvince *pro = self.provinceArr[indexPath.row];
    citiesControl.cities = pro.cities;
    
    [self.navigationController pushViewController:citiesControl animated:YES];

}

#pragma mark - 懒加载
- (NSArray *)provinceArr {

    if (_provinceArr == nil) {
        //获取plist
        NSString *path = [[NSBundle mainBundle] pathForResource:@"province.plist" ofType:nil];
        NSArray *dictArr = [NSArray arrayWithContentsOfFile:path];
        //创建可变数组保存模型
        NSMutableArray *arrM = [NSMutableArray array];
        for (NSDictionary *dict in dictArr) {
            // 创建模型对象"一个字典对应一个模型对象"
            //            HMApp *app = [[HMApp alloc] initWithDict:dict];
            HHProvince *province = [HHProvince provinceWithDict:dict];
            // 给模型的属性赋值
            //            app.icon = dict[@"icon"];
            //            app.name = dict[@"name"];
            // 把已经有值的模型对象添加到可变数组中
            [arrM addObject:province];
        }
        _provinceArr = arrM;
        
    }
    return _provinceArr;
}
@end
